package com.sand.service.Imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sand.dao.IBaseDao;
import com.sand.entity.AppointmentformEntity;
import com.sand.entity.CityEntity;
import com.sand.entity.CommunityEntity;
import com.sand.entity.InformationEntity;
import com.sand.service.ICityService;
import com.sand.util.PageModel;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service(value = "cityService")
public class CityServiceImp implements ICityService {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/3/003 16:14
 */
    //Dao层接口
    @Resource(name = "baseDao")
    private IBaseDao baseDao;

    //查询所有城市
    @Override
    public List<HashMap<String, Object>> queryAllCity() {
        ArrayList<HashMap<String, Object>> list= new ArrayList<>();
        //省
        List<CityEntity> cityTopList = baseDao.queryObject("from CityEntity t1 where t1.parentId=?0", 0);
        for (CityEntity c : cityTopList) {
            HashMap<String, Object> cityTopMap = new HashMap<>();
            cityTopMap.put("title",c.getName());
            cityTopMap.put("id",c.getRid());
            ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
            List<CityEntity> cityProList = baseDao.queryObject("from CityEntity t1 where t1.parentId=?0", c.getRid());
            for (CityEntity d : cityProList) {
                HashMap<String, Object> cityProMap = new HashMap<>();
                cityProMap.put("title",d.getName());
                cityProMap.put("id",d.getRid());
                list1.add(cityProMap);
            }
            cityTopMap.put("child",list1);
            list.add(cityTopMap);
        }
        return list;
    }

    //分页查询小区
    @Override
    public PageModel<HashMap<String, Object>> queryCityLimit(int limit, int page, String condition) {
        PageModel<HashMap<String, Object>> pm = new PageModel<HashMap<String, Object>>();

        //计算开始位置
        int begin=(page-1)*limit;
        //创建查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(CommunityEntity.class);
        //添加模糊查询条件
        if(condition!=null){
            List list = baseDao.queryObject("select t1.rid from CommunityEntity t1 where t1.communityName like '%"+condition+"%'");
            dc.add(Restrictions.in("rid",list));
        }

        List<Long> list1 = baseDao.queryObjectCommon(dc.setProjection(Projections.rowCount()));
        dc.setProjection(null);
        dc.setProjection(Projections.property("rid"));
        dc.addOrder(Order.desc("rid"));
        List list = baseDao.queryObjectLimit(dc, begin, limit);
        long i=list1.get(0);
        int count= (int) i;

        List<HashMap<String, Object>> list2 = baseDao.queryObject(" select new Map(" +
                "t1.rid as rid,t1.communityName as communityName,t1.communityAddress as communityAddress,t2.name as cityName,t2.parentId as parentId) " +
                "from CommunityEntity t1 inner join CityEntity t2 on t1.cityId=t2.rid where t1.rid in (?0)", list);
        for (HashMap<String, Object> map : list2) {
            List<HashMap<String, Object>> list3 = baseDao.queryObject("select new Map(t1.name as name,t1.parentId as parentId)" +
                    " from CityEntity t1 where t1.rid=?0", Integer.parseInt(map.get("parentId").toString()));
            HashMap<String, Object> map2 = list3.get(0);
            map.put("cityNamePro",map2.get("name"));
            List<String> list4 = baseDao.queryObject("select t1.name from CityEntity t1 where t1.rid=?0", Integer.parseInt(map2.get("parentId").toString()));
            map.put("cityNameTop",list4.get(0));
        }


        pm.setList(list2);
        pm.setPageNo(page);
        pm.setPageSize(limit);
        pm.setPagecount(count);
        return pm;
    }
    //根据id查询小区
    @Override
    public HashMap<String, Object> queryCommunityById(int id) {
        List<HashMap<String, Object>> list = baseDao.queryObject(" select new Map(" +
                "t1.rid as rid,t1.communityName as communityName,t1.communityAddress as communityAddress,t1.cityId as cityId," +
                "t2.parentId as parentId) " +
                "from CommunityEntity t1 inner join CityEntity t2 on t1.cityId=t2.rid where t1.rid=?0", id);
        HashMap<String, Object> map = list.get(0);
        List<CityEntity> list2 = baseDao.queryObject("from CityEntity t1 where t1.rid=?0", Integer.parseInt(map.get("parentId").toString()));
        map.put("city",list2.get(0));
        return map;
    }
    //添加小区
    @Override
    public boolean addCommunity(String data) {
        if(data!=null){
            //解析json数据
            CommunityEntity community = JSON.parseObject(data, CommunityEntity.class);
            boolean bln = baseDao.addObject(community);
            if(bln){
                return true;
            }
        }
        return false;
    }
    //删除小区
    @Override
    public boolean delCommunity(String data) {
        if(data!=null){
            //解析json数据
            List<CommunityEntity> list = JSON.parseArray(data, CommunityEntity.class);
            boolean bln = baseDao.delAllObject(list);
            if(bln){
                return true;
            }
        }
        return false;
    }
    //修改小区
    @Override
    public boolean updateCommunity(String data) {
        if(data!=null){
            //解析json数据
            CommunityEntity community = JSON.parseObject(data, CommunityEntity.class);
            boolean bln = baseDao.updateObject(community);
            if(bln){
                return true;
            }
        }
        return false;
    }
    //根据城市名查询id
    @Override
    public CityEntity queryCityByName(String cityName) {
        List<CityEntity> list = baseDao.queryObject("from CityEntity t1 where t1.parentId!=0 and t1.name=?0",cityName);
        return list.get(0);
    }
    //根据城市名模糊查询
    @Override
    public List<CityEntity> queryCityByLikeName(String cityName) {
        List list1 = baseDao.queryObject("select t1.rid from CityEntity t1 where t1.parentId=0");
        List<CityEntity> list = baseDao.queryObject("from CityEntity t1 where t1.parentId in (?0) and t1.name like '%"+cityName+"%'",list1);
        return list;
    }
    //根据小区名模糊查询
    @Override
    public List<HashMap<String, Object>> queryComByLikeName(String condition) {
        List<HashMap<String,Object>> list=new ArrayList<>();
        if(condition!=null){
            JSONObject obj = JSON.parseObject(condition);
            if(obj.get("name")!=null && !"".equals(obj.get("name")) && obj.get("name").toString().matches("[\u4E00-\u9FA5]+")){
                list = baseDao.queryObject("select new Map(t1.rid as rid,t1.communityName as comName,t2.name as cityName) " +
                        "from CommunityEntity t1 inner join CityEntity t2 on t1.cityId=t2.rid where t2.parentId=?0 and t1.communityName like '%" + obj.get("name") + "%'",Integer.parseInt(obj.get("cityId").toString()));
            }
        }
        return list;
    }

    //城市分级查询
    @Override
    public HashMap<String, Object> queryCityList(int id, int showWi,String condition) {
        HashMap<String, Object> map = new HashMap<>();

        switch (showWi){
            case 1:
                List<CityEntity> list = baseDao.queryObject("from CityEntity t1 where t1.parentId=?0", 0);
                List<CityEntity> list1 = baseDao.queryObject("from CityEntity t1 where t1.parentId=(select t2.parentId from CityEntity t2 where t2.rid=?0)", id);
                List<CityEntity> list2 = baseDao.queryObject("from CityEntity t1 where t1.parentId=?0", id);
                map.put("cityTopList",list);
                map.put("cityProList",list1);
                map.put("cityList",list2);
                break;
            case 2:
                List<CityEntity> list3 = baseDao.queryObject("from CityEntity t1 where t1.parentId=?0", id);
                List<CityEntity> list4 = baseDao.queryObject("from CityEntity t1 where t1.parentId=?0", list3.get(0).getRid());
                map.put("cityProList",list3);
                map.put("cityList",list4);
                break;
            case 3:
                List<CityEntity> list5 = baseDao.queryObject("from CityEntity t1 where t1.parentId=?0", id);
                map.put("cityList",list5);
                break;
            case 4:
                if(condition!=null && !"".equals(condition) && condition.matches("[\u4E00-\u9FA5]+")){
                    List<CommunityEntity> list6 = baseDao.queryObject("select t1 as comName from CommunityEntity t1 inner join CityEntity t2 on t1.cityId=t2.rid where t1.communityName like '%"+condition+"%' and t2.rid=?0", id);
                    map.put("comList",list6);
                }
                break;
        }
        return map;
    }
}
